package modes

import (
	"fmt"
	"time"
)

type AgreeSign struct {
	Id          int64  `json:"agr_id" xorm:"id" form:"agree_id"`            //  id
	BillNo      string `json:"bill_no" xorm:"bill_no" form:"bill_no"`       //  订单号
	PId         string `json:"agr_pid" xorm:"pid" form:"pid"`               //  项目
	CId         string `json:"agr_cid" xorm:"cid" form:"cid"`               //  班型id
	StuId       int64  `json:"student_id" xorm:"stu_id" form:"stu_id"`      //  学员Id
	State       int    `json:"agr_state" xorm:"state"`                      //  是否签署协议否 1  是  2
	UserName    string `json:"username" xorm:"username" form:"username"`    //  学员姓名
	Card        string `json:"agr_card" xorm:"card" form:"card"`            //  身份证号
	AuType      int64  `json:"agr_au_type" xorm:"au_type" form:"au_type"`   //  审核类型 1 未审核 2 自动审核 3坐席审核
	Type        int64  `json:"type" xorm:"type" form:"type"`                //  协议类型 1 前端 2 后端
	SAt         int64  `json:"agr_s_at" xorm:"s_at" form:"s_at"`            //  签署时间
	At          int64  `json:"agr_at" xorm:"at"`                            //  插入时间
	Note        string `json:"agr_note" xorm:"note" form:"note"`            //  备注
	FrontImg    string `json:"front_img" xorm:"front_img" form:"front_img"` //  身份正面
	BackImg     string `json:"back_img" xorm:"back_img" form:"back_img"`    //  身份反面
	Cou         int64  `json:"agr_cou" xorm:"cou"`                          //  默认身份认证机会次数
	OrderId     int64  `json:"agr_order_id" xorm:"order_id"`                //  crm_order中的订单id
	Uname       string `json:"uname" xorm:"uname"`                          //  辅助坐席
	Phone       string `json:"phone" xorm:"phone"`                          //  学员手机号
	Pname       string `json:"pname" xorm:"pname"`                          //  项目名称
	Cname       string `json:"cname" xorm:"cname"`                          //  班型名称
	TerminalIds string `json:"terminal_ids" xorm:"terminal_ids"`            //  哪个端进行签署协议APP = 1 PC  = 2
	Url         string `json:"url" xorm:"url"`                              //  pdf文件地址
}

func (this *AgreeSign) TableName() string {
	return "agree_sign"
}

func (this *AgreeSign) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(2).Insert(this)
}

func (this *AgreeSign) Get() (bool, error) {
	return Db(2).Get(this)
}
func (this *AgreeSign) GetListByOrderId() ([]AgreeSign, error) {
	list := make([]AgreeSign, 0)
	err := Db(2).Where("state=2 and order_id=? ", this.OrderId).Find(&list)
	return list, err

}

func (this *AgreeSign) GetList() (bool, error) {
	list := make([]AgreeSign, 0)
	err := Db(2).Where("(state=2 or state=3)and username=? and card=? ", this.UserName, this.Card).Find(&list)
	return len(list) > 0, err

}

type AgreeSignUser struct {
	Id     int64  `json:"agr_id" xorm:"id" form:"agree_id"` //  id
	Cou    int64  `json:"agr_cou" xorm:"cou"`               //  默认身份认证机会次数
	Url    string `json:"url"`                              //  查看协议的死页面的地址
	Type   int64  `json:"type" xorm:"type"`                 //  协议的类型
	BillNo string `json:"bill_no" xorm:"bill_no"`           //  账单号
}

/**
未签署协议的列表
*/
func (this *AgreeSign) GetNoSignList() ([]AgreeSignUser, error) {
	list := make([]AgreeSignUser, 0)
	err := Db(2).Table(this.TableName()).Where("state=1 and stu_id=? and terminal_ids like '%"+this.TerminalIds+"%' ", this.StuId).Find(&list)
	return list, err
}

func (this *AgreeSign) GetPhone(uid int64) ([]AgreeSign, error) {
	list := make([]AgreeSign, 0)
	w := fmt.Sprintf("stu_id = %d", uid)
	err := Db(2).Where(w).Find(&list)
	return list, err
}

func (this *AgreeSign) GetAgree(uid int64, cid string) (bool, error) {
	where := fmt.Sprintf("stu_id = %d and find_in_set(%s,cid) and state = 0", uid, cid)
	return Db(2).Where(where).Get(this)
}

func (this *AgreeSign) delete(where string) (int64, error) {
	return Db(2).Where(where).Delete(this)
}
func (this *AgreeSign) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *AgreeSign) Count(where string) (int64, error) {
	var val LiveClass
	return Db(2).Where(where).Count(&val)
}
func (this *AgreeSign) update(where string, field string) (int64, error) {
	return Db(2).Where(where).Cols(field).Update(this)
}

func (this *AgreeSign) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *AgreeSign) AgreeSignIdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *AgreeSign) where(fage, count, page int, where, field string) ([]AgreeSign, error) {
	list := make([]AgreeSign, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(2).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(2).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

type AgreesPro struct {
	Class `json:"cla" xorm:"extends"`

	Project `json:"pro" xorm:"extends"`
}

func (this *AgreesPro) Lookups(slicel []string, val *Pager) error {
	list := make([]AgreesPro, 0)
	val.TName = "user_class"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.id", val.TName)
	default:
		field = fmt.Sprintf("%s.id", val.TName)
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(1).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(1).Table(val.TName).
		Join("LEFT", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		In("livecid.cid", slicel).
		Where(val.ToWhere()).
		//Where(swhere).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	fmt.Println(list)
	return err
}

func (this *AgreeSign) ListByIds(ids []int64) ([]AgreeSign, error) {
	list := make([]AgreeSign, 0)
	err := Db(2).Table(this.TableName()).In("order_id", ids).Find(&list)
	return list, err
}
